package com.lfsenior;

import net.sf.ehcache.Cache;
import net.sf.ehcache.Element;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.ehcache.EhCacheCacheManager;
import org.springframework.stereotype.Service;

/**
 * Created by dlupan on 2018/10/8
 */
@Service
public class TestService {
    @Autowired
    CacheManager cacheManager;

    public User getUser(){
        return get();
    }
    
    public User get(){
        Cache userCache = ((EhCacheCacheManager) this.cacheManager).getCacheManager().getCache("UserCache");
        Element element = userCache.get("getUser");
        if(null==element){
            System.out.println("没走缓存");
            return set("CACHE1");
        }
        return (User) element.getValue();

    }
    public User set(String name){
        Cache userCache = ((EhCacheCacheManager) this.cacheManager).getCacheManager().getCache("UserCache");
        User user=new User();
        user.setName(name);
        userCache.put(new Element("getUser",user));
        return user;
    }
}
